package org.jeecg.chase.common.supper.scene;

import org.jeecg.chase.common.base.annotation.QueryConfig;
import org.jeecg.chase.common.base.annotation.Scene;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.type.filter.AnnotationTypeFilter;

/**
 * @author leikai
 * @version 1.0
 * @description: 解析
 * @date 2021/07/23/0023 16:12
 */
@Configuration
public class ScannerConfigurer implements BeanFactoryPostProcessor {

    private static final Logger logger = LoggerFactory.getLogger(ScannerConfigurer.class);

    /**
     * 扫描注解
     *
     * @param configurableListableBeanFactory
     * @return void
     * @author leikai
     * @date 2021/07/26/0026 16:47
     */

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

        logger.info("扫描query=======================================================");
        ListQueryScanner listQueryScanner = new ListQueryScanner((BeanDefinitionRegistry) configurableListableBeanFactory);
        SceneAnnotationUtils.processBaseDataBeanDefinition(listQueryScanner.findCandidateComponents("org.jeecg.chase"));

        SceneScanner sceneScanner = new SceneScanner((BeanDefinitionRegistry) configurableListableBeanFactory);
        SceneAnnotationUtils.processSceneClass(sceneScanner.findCandidateComponents("org.jeecg.chase"));
    }

    /**
     * 扫描场景
     *
     * @author leikai
     * @date 2021/07/26/0026 16:49
     * @return
     */
    class SceneScanner extends ClassPathBeanDefinitionScanner {

        public SceneScanner(BeanDefinitionRegistry registry) {
            super(registry);
        }

        @Override
        public void registerDefaultFilters() {
            this.addIncludeFilter(new AnnotationTypeFilter(Scene.class));
        }
    }

    /**
     * query
     *
     * @author leikai
     * @date 2021/08/04/0004 17:50
     * @return
     */
    class ListQueryScanner extends ClassPathBeanDefinitionScanner {

        public ListQueryScanner(BeanDefinitionRegistry registry) {
            super(registry);
        }

        @Override
        public void registerDefaultFilters() {
            this.addIncludeFilter(new AnnotationTypeFilter(QueryConfig.class));
        }
    }
}
